//package com.tree.common.config.redis;
//
//import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
//import com.fasterxml.jackson.annotation.JsonAutoDetect;
//import com.fasterxml.jackson.annotation.PropertyAccessor;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import org.springframework.cache.annotation.CachingConfigurerSupport;
//import org.springframework.cache.annotation.EnableCaching;
//import org.springframework.cache.interceptor.KeyGenerator;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.Primary;
//import org.springframework.data.redis.cache.RedisCacheManager;
//import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.data.redis.serializer.RedisSerializer;
//import org.springframework.data.redis.serializer.StringRedisSerializer;
//
//import java.lang.reflect.Method;
//
//
///**
// * @ClassName com.demo.common.config.redis
// * Description: <类功能描述>. <br>
// * <p>
// * <使用说明>
// * </p>
// * @Author tree
// * @Date 2019-05-25 20:52
// * @Version 1.0
// */
//@Configuration
//@EnableCaching
//public class RedisConfig extends CachingConfigurerSupport {
//
//
//    /**
//     * 自定义缓存Key的生成策略
//     * 若想使用这个key，只需要注解上KeyGenerator的值设置为KeyGenerator即可
//     * @return 自定义策略生成的key
//     */
//    @Bean
//    @Override
//    public KeyGenerator keyGenerator(){
//        return new KeyGenerator(){
//
//            @Override
//            public Object generate(Object target, Method method, Object... params) {
//                StringBuffer sb = new StringBuffer();
//                sb.append(target.getClass().getName());
//                sb.append(method.getName());
//                for(Object param : params){
//                    sb.append(param.toString());
//                }
//                return sb.toString();
//            }
//        };
//    }
//
//    /**
//     * 缓存管理器
//     *
//     */
//    @Bean
//    public RedisCacheManager cacheManager(JedisConnectionFactory jedisConnectionFactory){
//        return RedisCacheManager.create(jedisConnectionFactory);
//    }
//
//    @Bean
//    @Primary
//    public RedisTemplate<String,Object> redisTemplate(JedisConnectionFactory jedisConnectionFactory){
//        //设置序列化
//        FastJsonRedisSerializer<Object> jackson2JsonRedisSerializer = new FastJsonRedisSerializer<>(Object.class);
//        ObjectMapper om = new ObjectMapper();
//        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
//        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
//
//        //配置redistemplate
//        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
//        redisTemplate.setConnectionFactory(jedisConnectionFactory);
//        RedisSerializer<String> stringRedisSerializer = new StringRedisSerializer();
//        //key序列化
//        redisTemplate.setKeySerializer(stringRedisSerializer);
//        //value以fastjson序列化
//        redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
//        redisTemplate.setHashKeySerializer(stringRedisSerializer);
//        redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
//        redisTemplate.afterPropertiesSet();
//        return redisTemplate;
//    }
//
//
//}
